package com.woniu.controller;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Article;
import com.woniu.pojo.Mark;
import com.woniu.service.IArticleService;
import com.woniu.vo.QuerySearchArticleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author doujiao
 * @since 2022-10-11
 */
@RestController
@RequestMapping("/article")
@Api(tags = "文章资讯模块")
public class ArticleController extends BaseController{

    @Resource
    private IArticleService articleService;

    /**
     * 条件搜索分页查询
     */
    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("条件搜索分页查询")
    public Result queryByPage(@PathVariable String currentPage, @PathVariable String pageSize,
                              @RequestBody QuerySearchArticleVo articleVo){
        PageResult pageResult=articleService.queryByPage(currentPage,pageSize,articleVo);
        return toDataResult(pageResult);
    }
    /**
     * 根据id查询一条数据
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询一条数据")
    public Result queryById(@PathVariable Integer id){
        //转long
        Long id1 = Long.valueOf(id);
        Article article = articleService.getById(id1);
        return toDataResult(article);
    }

    /**
     * 增加一条文章
     */
    @PostMapping
    @ApiOperation("增加一条文章")
    public Result add(@RequestBody Article article){
        boolean save = articleService.save(article);
        return toResult(save);
    }

    /**
     * 编辑一条文章
     */
    @PutMapping
    @ApiOperation("编辑一条文章")
    public Result edit(@RequestBody Article article){
        boolean result = articleService.updateById(article);
        return toResult(result);
    }
    /**
     * 根据id删除一条文章
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除一条文章")
    public Result removeById(@PathVariable Integer id){
        //转Long
        Long id1 = Long.valueOf(id);
        boolean result = articleService.removeById(id1);
        return toResult(result);
    }

    @ApiOperation("查询轮播图排序的最大值")
    @GetMapping("/sort")
    public Result queryMaxSortByCourseId(){
        Integer maxSort = articleService.queryMaxSortByCourseId();
        return toDataResult(maxSort);
    }

    @ApiOperation("修改轮播图发布状态")
    @PutMapping("/sta/{articleId}")
    public Result getByValAndCourseId(@PathVariable Long articleId){
        boolean flag = articleService.saveStatus(articleId);
        return toResult(flag);
    }


    @ApiOperation("修改文章收藏数量")
    @PutMapping("/collectNum/{articleId}")
    public Result updateCillectNumByMark(@PathVariable Long articleId){
        boolean flag = articleService.updateCollectNumByMark(articleId);
        return toResult(flag);
    }

}
